package com.my.bld.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.my.bld.model.Manage;
import com.my.bld.model.User;
import com.my.bld.service.ManageService;
import com.my.bld.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("loginAction")
@Scope("prototype")
public class LoginAction extends ActionSupport {

//============依赖注入start================
		@Autowired
		private ManageService manageService;//管理
		@Autowired
		private UserService userService;//用户
		
		
		//===========依赖注入end==================
		
		//-------------------------华丽分割线---------------------------------------------
		
		//============自定义参数start=============
		private Integer role;
		public Integer getRole() {
			return role;
		}
		public void setRole(Integer role) {
			this.role = role;
		}

		private String userName;//用户名
		private String passWord;//密码
		private String code;//统一社会信用代码： 
		private Integer type;//1新型材料、2生物医药、3富硒食品、4安康丝绸、5清洁能源、6装备制造
		private String zczb;//注册资本
		private String name;//企业名称
		private String djjg;//登记机关
		private String address;//地址
		private String  fr;//法定代表人
		private String info;//企业简介包含 经营内容和数据分析
		private Integer isSh;//是否审核 0否 1是
		//============自定义参数end=============
		
		//-------------------------华丽分割线---------------------------------------------
		
		public String getUserName() {
			return userName;
		}
		public void setUserName(String userName) {
			this.userName = userName;
		}
		public String getPassWord() {
			return passWord;
		}
		public void setPassWord(String passWord) {
			this.passWord = passWord;
		}
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		public Integer getType() {
			return type;
		}
		public void setType(Integer type) {
			this.type = type;
		}
		public String getZczb() {
			return zczb;
		}
		public void setZczb(String zczb) {
			this.zczb = zczb;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getDjjg() {
			return djjg;
		}
		public void setDjjg(String djjg) {
			this.djjg = djjg;
		}
		public String getAddress() {
			return address;
		}
		public void setAddress(String address) {
			this.address = address;
		}
		public String getFr() {
			return fr;
		}
		public void setFr(String fr) {
			this.fr = fr;
		}
		public String getInfo() {
			return info;
		}
		public void setInfo(String info) {
			this.info = info;
		}
		public Integer getIsSh() {
			return isSh;
		}
		public void setIsSh(Integer isSh) {
			this.isSh = isSh;
		}

		//============文件上传start=============
		private File file;
		//提交过来的file的名字
	    private String fileFileName;
	    //提交过来的file的MIME类型
	    private String fileContentType;
	    public File getFile() {
			return file;
		}
		public void setFile(File file) {
			this.file = file;
		}
		public String getFileFileName() {
			return fileFileName;
		}
		public void setFileFileName(String fileFileName) {
			this.fileFileName = fileFileName;
		}
		public String getFileContentType() {
			return fileContentType;
		}
		public void setFileContentType(String fileContentType) {
			this.fileContentType = fileContentType;
		}
		//============文件上传end=============
		
		//-------------------------华丽分割线---------------------------------------------

		public String login() {
			return "login";
		}
		
		public String home() {
			return "success";
		}
		
       //登录
		public String toLogin(){
			if(role != null && role==2){//销售
				String hql = "from User where userName = :userName and passWord = :passWord and isDelete = 0 ";
				  Map<String,Object> alias = new HashMap<String,Object>();
				  alias.put("userName",userName);
				  alias.put("passWord", passWord);
				  List<User> userList= userService.getByHQL(hql, alias);
				if(userList.size()>0){
					//将用户信息放入session
					HttpSession session = ServletActionContext.getRequest()
							.getSession();
					session.setAttribute("userId",userList.get(0).getId() );
					session.setAttribute("user", userList.get(0));
					session.setAttribute("role", 1);
					return "index";
				}else{
					return "login";
				}
			}else{
				
					String hql = "from Manage where name = :userName and passWord = :passWord";
					  Map<String,Object> alias = new HashMap<String,Object>();
					  alias.put("userName",userName);
					  alias.put("passWord", passWord);
					List<Manage> manageList=manageService.getByHQL(hql, alias);
					if(manageList.size()>0){
						//将用户信息放入session
						HttpSession session = ServletActionContext.getRequest()
								.getSession();
						session.setAttribute("role", 2);
						session.setAttribute("userName", manageList.get(0).getRealName());
						session.setAttribute("userId2",manageList.get(0).getId() );
						session.setAttribute("manage", manageList.get(0));
						session.setAttribute("roleId", role);
						return "index";
					}else{
						return "login";
					}
				}
			
			
		}
		
		   //退出
			public String tuichu() {
				ActionContext ac = ActionContext.getContext();
				Map session = ac.getSession();
				session.remove("userName");
				session.remove("userId");
				return "login";
			}
	
}
